//author marcel

package ch.sfug.anim.transition
{
	import ch.sfug.anim.tween.property.Fader;
	import ch.sfug.events.TweenEvent;
	
	import flash.display.Sprite;

	public class FadeTransition extends Transition
	{
		private var pt:Fader;
		private var _ease:Function = null;
		
		public function FadeTransition(clip:Sprite=null, time:Number=1000, type:String = TransitionType.OUT )
		{ 
			super(clip, time, type);
		}
		
		
		override protected function prepareForTransition():void 
		{
			super.prepareForTransition();
			
			if(pt == null)
			{
				pt = new Fader(super.clip, super.time);
				pt.addEventListener(TweenEvent.TWEEN_UPDATE, onTransitionUpdate);
				pt.addEventListener(TweenEvent.TWEEN_END, onTransitionEnd);
			}
			
		}
				
		override public function startTransition():void 
		{
			prepareForTransition();
			onTransitionStart();
			
			if(super.type == "out")
			{
				pt.fadeFromTo(1, 0, _ease);
			}
			else
			{
				pt.fadeFromTo(0, 1, _ease);
			}
		}
		
		
		override public function stopTransition():void 
		{
			if(pt != null)
			{
				pt.stop();
				pt.removeEventListener(TweenEvent.TWEEN_UPDATE, super.onTransitionUpdate);
				pt.removeEventListener(TweenEvent.TWEEN_END, super.onTransitionEnd);
				pt = null;
			}
			
			super.cleanUp();
		}
		
		// getters and setters
		public function set ease( val:Function ):void
		{
			_ease = val;
		}
				
	}
}

